import { Component, OnInit } from '@angular/core';
import { FormBuilder, FormGroup, Validators } from '@angular/forms';
import { HttpClient } from '@angular/common/http';
import {ActivatedRoute, Router} from '@angular/router';

@Component({
  selector: 'app-login',
  templateUrl: './login.component.html',
  styleUrls: ['./login.component.css']
})
export class LoginComponent implements OnInit {
  validateForm!: FormGroup;

  tabs = [1, 2, 3];


  submitForm(): void {
    for (const i in this.validateForm.controls) {
      this.validateForm.controls[i].markAsDirty();
      this.validateForm.controls[i].updateValueAndValidity();
    }
    const userName = this.validateForm.value.userName;
    const password = this.validateForm.value.password;
    // 登录请求
    this.loginCheck(userName, password);
    // 根据userName和password验证登录
  }

  constructor(private fb: FormBuilder,
              private http: HttpClient,
              private route: ActivatedRoute,
              private router: Router,
  ) {}

  ngOnInit(): void {
    this.validateForm = this.fb.group({
      userName: [null, [Validators.required]],
      password: [null, [Validators.required]],
      remember: [true]
    });
  }

  loginCheck(userNameParam: string, passWordParam: string): void {
    const  loginUrl = '/api/login/check';
    localStorage.setItem('token', userNameParam);
    const paramsObj = {
      userName: userNameParam,
      passWord: passWordParam,
    };
    const  requestParams = {
      params: paramsObj
    }
    this.http.post(loginUrl, requestParams, {}).subscribe(data => {
       console.log(data);
     });
  }

  tabChange(index: number): void {
    console.log(index);
  }

}
